import { GetDataByMeter } from '@/api/dashboard'
import { GetComboxList, GetProcedureButtonLimit } from '@/api/procedure'
import { Dictionary, SettingType } from '@/interface/Basic'
import { AxiosResponse } from 'axios'
// 转换成 el-table 组件需要的数据格式
export function FormatChannelData(channelData: Dictionary<string>[], tableColumn: string[]) {
  return channelData.map(row => {
    const obj: Dictionary<string> = {}
    Object.values(row).forEach((item, index) => {
      obj[tableColumn[index]] = item
    })
    return obj
  })
}
// 向 channel 中填入数据，属性不存在，填入空值
export function FillInChannelData(channelData: Dictionary<string>[], origin: Dictionary<string>) {
  return channelData.map(channel => {
    Object.keys(channel).forEach(key => {
      if (Object.prototype.hasOwnProperty.call(origin, key)) channel[key] = origin[key]
    })
    return channel
  })
}
export function CaptureChannelData(
  channelData: Dictionary<string>[],
  origin: Dictionary<string>[]
) {
  return origin.reduce((accu, next, index) => {
    Object.keys(next).forEach((key, idx) => {
      const v_key = Object.keys(channelData[index])[idx]
      next[key] = channelData[index][v_key]
    })
    return { ...accu, ...next }
  }, {})
}
// 向 task-tab 组件的数据源中填入值
export function FillInTaskTabData(target: SettingType[], origin: Dictionary<string>) {
  return target.map(row => {
    if (Object.prototype.hasOwnProperty.call(origin, row.key)) {
      row.value = origin[row.key]
    }
    return row
  })
}
export function CaptureTaskTabData(target: SettingType[]) {
  const result: Dictionary<string> = {}
  target.forEach(item => {
    result[item.key] = item.value
  })
  return result
}

// 获取 ComboxOptions
export interface ComboxOptionType {
  Savetime: string
  Value: Dictionary<string>
}
export interface ComboxListType {
  Label: string
  options: ComboxOptionType[]
}
export type ComboxOptionsDict = Dictionary<ComboxOptionType[]>
export function GetComboxOptions(meter: string): Promise<ComboxOptionsDict> {
  return new Promise(resolve => {
    GetComboxList({ Number: meter }).then(
      (res: AxiosResponse<{ ReturnData: ComboxListType[] }>) => {
        const { ReturnData } = res.data
        const result: ComboxOptionsDict = ReturnData.reduce((accu, next) => {
          const obj: ComboxOptionsDict = {}
          obj[next.Label] = next.options
          return { ...accu, ...obj }
        }, {})
        resolve(result)
      }
    )
  })
}

// 获取主页面参数，包括【提取需要的公共参数】以及【更新当前页权限】
export function GetInitialParams(
  meter: string,
  target: Dictionary<string>
): Promise<{ limits: string; params: Dictionary<string> }> {
  return new Promise(resolve => {
    GetDataByMeter(meter).then((res: AxiosResponse<{ ReturnData: Dictionary<string> }>) => {
      const { ReturnData } = res.data
      Object.keys(target).forEach(key => {
        if (ReturnData[key]) {
          target[key] = ReturnData[key]
        }
      })
      resolve({
        limits: ReturnData.AAF002 ? ReturnData.AAF002 : '',
        params: target
      })
    })
  })
}
export function GenerateCurPageLimits(
  limitCode: string,
  procedure: string,
  target: Dictionary<boolean>
): Promise<Dictionary<boolean>> {
  return new Promise(resolve => {
    GetProcedureButtonLimit({ SeNumber: limitCode, DatabaseLabel: procedure }).then(
      (res: AxiosResponse<{ ReturnData: Dictionary<boolean> }>) => {
        const { ReturnData } = res.data
        Object.keys(ReturnData).forEach(key => {
          if (Object.hasOwnProperty.call(target, key)) {
            target[key] = ReturnData[key]
          }
        })
        resolve(target)
      }
    )
  })
}

// 导入CSV文件，转换中英文键值
/**
 * @param target 需要的数据的中英文对照表
 * @param origin 源数据，键为中文，需要按照中英文对照表将键转换为英文
 */
export function TransferCnKeyToEnKey(target: Dictionary<string>, origin: Dictionary<string>) {
  const res: Dictionary<string> = {}
  Object.keys(target).forEach(cn_key => {
    res[target[cn_key]] = origin[cn_key] ? origin[cn_key] : ''
  })
  return res
}
